"""
文件读取
"""


# 文件打开
f = open("D:/test.txt", "r", encoding="UTF-8")
print(f)


# 文件读取 - read
f = open("D:/test.txt", "r", encoding="UTF-8")
content = f.read() # 不传参数，默认读取全部
print(content)
# 打印
# itheima itcaset heima 传智教育
# 黑马程序员 itheima itcaset heima
# 传智教育 黑马程序员 itheima itcaset

f = open("D:/test.txt", "r", encoding="UTF-8")
content = f.read(7) # 读取7个字节
print(content) # 打印 itheima
print("--------------------------")

# 文件读取 - readline
f = open("D:/test.txt", "r", encoding="UTF-8")
line = f.readline()
print(line) # 打印 itheima itcaset heima 传智教育
line = f.readline()
print(line) # 打印 黑马程序员 itheima itcaset heima
line = f.readline()
print(line) # 打印 传智教育 黑马程序员 itheima itcaset
print("--------------------------")

# 文件读取 - readlines
f = open("D:/test.txt", "r", encoding="UTF-8")
lines = f.readlines()
print(lines)   # ['itheima itcaset heima 传智教育\n', '黑马程序员 itheima itcaset heima\n', '传智教育 黑马程序员 itheima itcaset']
print(type(lines)) # <class 'list'>
print("--------------------------")


# 文件读取 - for循环读取
f = open("D:/test.txt", "r", encoding="UTF-8")
for line in f:
    print(line)


# 文件读取 - close
import time
f = open("D:/test.txt", "r", encoding="UTF-8")
# 让程序休眠
# time.sleep(100000)
print("--------------------------")


# 文件读取 - with open语法
with open("D:/test.txt", "r", encoding="UTF-8") as f:
    print(f.readlines())

